package com.beardedhen.harmonybootstrap.utils;


import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;


/**
 * The type Dimen utils.
 */
public class DimenUtils {

    /**
     * Pixels from dp resource float.
     *
     * @param context the current context
     * @param res     the dimension resource holding a DP value
     * @return the size in pixels
     * @throws NotExistException  the not exist exception
     * @throws WrongTypeException the wrong type exception
     * @throws IOException        the io exception
     */
    public static float pixelsFromDpResource(Context context,  int res) throws NotExistException, WrongTypeException, IOException {

        return context.getResourceManager().getElement(res).getFloat();
    }

    /**
     * Pixels from sp resource float.
     *
     * @param context the context
     * @param sizeRes the size res
     * @return the float
     * @throws NotExistException  the not exist exception
     * @throws WrongTypeException the wrong type exception
     * @throws IOException        the io exception
     */
    public static float pixelsFromSpResource(Context context, int sizeRes) throws NotExistException, WrongTypeException, IOException {

        final ResourceManager res = context.getResourceManager();
        return res.getElement(sizeRes).getFloat();
    }

    /**
     * Dp to pixels int.
     *
     *Converts density-independent pixels to pixels
     * @param context the current context
     * @param dip the dips
     * @return size in pixels
     */
    public static int dpToPixels(Context context, float dip) {
        return (int) (dip * DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().densityPixels);
    }
}

